package easamples;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.sparx.Collection;
import org.sparx.Element;
import org.sparx.Repository;


public class ManagePackagesExample implements IExample
{
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return "Add and Manage Packages";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#getDescription()
	 */
	public String getDescription()
	{
		return "Example illustrating how to add a Model or a Package.<br/><br/><b>Related APIs</b><ul><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?package_2.htm'>Package API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?element2.htm'>Element API</a></li><li><a href='http://www.sparxsystems.com.au/EAUserGuide/index.html?repository3.htm'>Repository API</a></li></ul>";
		//return "Not implemented (yet)";
	}

	/*
	 * (non-Javadoc)
	 * @see org.sparx.javaexample.test.IExample#runExample(java.io.File, javax.swing.DefaultListModel)
	 */
	public void runExample( File projectFile, ExampleUI uiFrame )
	{
		Repository rep = null;
		DefaultListModel outputList = uiFrame.getOutputListModel();
		
		try
		{
			// Create a repository object - This will create a new instance of EA
			rep = new Repository();
			
			// Attempt to open the provided file
			if ( rep.OpenFile(projectFile.getAbsolutePath()) )
			{
				// ==================================================
				// ADD A MODEL
				// ==================================================
				Collection<org.sparx.Package> models = rep.GetModels();
				org.sparx.Package newModel = models.AddNew( "AdvancedModel", "" );
				newModel.Update();
				models.Refresh();
				
				int newModelID = newModel.GetPackageID();
				outputList.addElement( "Added new model: " + newModel.GetName() 
						+ " (PackageID=" + newModelID + ")" );
				
				// ==================================================
				// ADD A SUBPACKAGE TO THE MODEL
				// ==================================================
				Collection<org.sparx.Package> packages = newModel.GetPackages();
				
				org.sparx.Package subPackage 
					= packages.AddNew( "SubPackage", "Class" );

				subPackage.Update();
				
				// GetElement returns the underlying element representation of the package
				// The initial Update() of the package must be called before we can access the'
				// underlying element.
				Element packageMetaElement = subPackage.GetElement();
				packageMetaElement.SetStereotype( "system" );
				subPackage.Update();
				
				packages.Refresh();

				outputList.addElement( "Added subpackage '" + subPackage.GetName() 
						+ "' under model '" + newModel.GetName() + "'" );
				
				packageMetaElement = null;
				subPackage = null;
				newModel = null;
				
				// List all models in the collection, deleting the one we added
				for ( short i = 0 ; i < models.GetCount() ; i++ )
				{
					org.sparx.Package currentModel = models.GetAt( i );
					String currentModelName = currentModel.GetName();
					int currentModelID = currentModel.GetPackageID();
					outputList.addElement( "Model: " + currentModelName );
					
					if ( currentModelID == newModelID )
					{
						models.DeleteAt( i, false );
						outputList.addElement( "Deleted model " + currentModelName
								+ " (PackageID=" + currentModelID + ")" );
					}
					
				}
				
				
				outputList.addElement( "Done!" );
			}
			else
			{
				// If the file couldn't be opened then notify the user
				String message = "EA was unable to open the file '";
				message += projectFile.getAbsolutePath();
				message += "'.";
				JOptionPane.showMessageDialog( uiFrame, message, "Invalid File", JOptionPane.ERROR_MESSAGE );
				outputList.addElement( message );
			}
		}
		catch ( UnsatisfiedLinkError ule )
		{
			String message = "Unable to communicate with EA.\nPlease ensure that the path to SSJavaCOM.dll included in java.library.path";
			JOptionPane.showMessageDialog( uiFrame, message, "Unable to link to SSJavaCOM.dll", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			String message = "An unhandled exception was thrown: " + e.getMessage();
			JOptionPane.showMessageDialog( uiFrame, message, "Unhandled Exception", JOptionPane.ERROR_MESSAGE );
			outputList.addElement( message );
		}
		finally
		{
			if ( rep != null )
			{
				// Clean up
				rep.CloseFile();
				rep.Exit();
				rep.destroy();
			}
		}
	}

}
